package com.googlecode.rulio.rendering.html;

import com.googlecode.rulio.Action;
import com.googlecode.rulio.ExecutionReport;
import com.googlecode.rulio.Rules;
import com.googlecode.rulio.rendering.Renderer;
import com.googlecode.totallylazy.Pair;
import com.googlecode.totallylazy.Predicate;

import java.util.Collection;
import java.util.List;

class Renderers {
    public static Renderer<Collection> collectionRenderer(final Renderer<Object> renderer) {
        return new Renderer<Collection>() {
            public String render(Collection object) {
                StringBuilder builder = new StringBuilder();

                for (Object o : object) {
                    builder.append(renderer.render(o));
                }

                return builder.toString();
            }
        };
    }

    public static Renderer<List<Pair<Predicate, Action>>> listRenderer() {
        return new Renderer<List<Pair<Predicate, Action>>>() {
            public String render(List<Pair<Predicate, Action>> object) {
                return "Actions";
            }
        };
    }

    public static Renderer<ExecutionReport> executionReportRenderer(final Renderer<Object> renderer) {
        return new Renderer<ExecutionReport>() {
            public String render(ExecutionReport report) {
                StringBuilder builder = new StringBuilder();
                builder.append("<p " + (report.executed ? "class=\"heading\"" : (report.matched ? "class=\"matchedNotExecuted\"" : "class=\"notMatched\"")) + ">" + renderer.render(report.predicate) + " => " + renderer.render(report.action) + "</p>\n" +
                        "<div class=\"content\">");

                if (report.results.length > 0) {
                    builder.append("<table width='100%'>");
                    for (Object result : report.results) {
                        builder.append("<tr style='padding:5px'><td style='border: 1px solid #000000'>");
                        builder.append(renderer.render(result));
                        builder.append("</td></tr>");
                    }

                    builder.append("</table>");
                }
                builder.append("</div>");
                return builder.toString();
            }
        };
    }

    public static Renderer<Rules> rulesRenderer() {
        return new Renderer<Rules>() {
            public String render(Rules rules) {
                return "Rules (Execute " + (rules.executeAllMatching() ? "all matching" : "first matching") + ")";
            }
        };
    }

    public static Renderer<Action> actionRenderer() {
        return new Renderer<Action>() {
            public String render(Action action) {
                return action.toString();
            }
        };
    }


    public static Renderer<Object> defaultRenderer() {
        return new Renderer<Object>() {
            public String render(Object object) {
                return "<i style='color:gray'>" + object + "</i>";
            }
        };
    }
}
